/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.base.web;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.mapper.JsonMapper;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.base.entity.StoreCare;
import com.chlitina.store.modules.base.entity.StoreSelfCare;
import com.chlitina.store.modules.base.service.StoreCareService;
import com.chlitina.store.modules.expert.entity.Care;
import com.chlitina.store.modules.expert.entity.CareStoreType;
import com.chlitina.store.modules.expert.utils.ExpertUtils;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

/**
 * 店面护理Controller
 * @author liujiang
 * @version 2013-09-26
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/base/storeCare")
public class StoreCareController extends BaseController {

	@Autowired
	private StoreCareService storeCareService;
	
	@ModelAttribute
	public StoreCare get(@RequestParam(required=false) Long id) {
		if (id != null){
			return storeCareService.get(id);
		}else{
			return new StoreCare();
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "storeCareData")
	public String storeCareData(@RequestParam(required=false) int type, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		Set<Map<String, Object>> set = Sets.newHashSet();
		String storeTypeId = StoreUtils.getStore().getStoreType().getId().toString();
		List<StoreCare> careList = storeCareService.findByStatus(type);
		for (StoreCare care : careList) {
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", care.getId());
			if(StoreCare.TYPE_SYS == type) {
				if( care.getCare().getStatus() == Care.FLAG_DISABLE){
					continue;
				}
				String storeTypeIds = "";
				for(CareStoreType cst : care.getCare().getStoreTypeList()){
					storeTypeIds += cst.getStoreType().getId() + ",";
				}
				if(!storeTypeIds.contains(storeTypeId)){
					continue;
				}
				map.put("name", care.getCare().getName());
			} else {
				if( care.getStoreSelfCare().getStatus() == StoreSelfCare.FLAG_DISABLE){
					continue;
				}
				map.put("name", care.getStoreSelfCare().getName());
			}
			set.add(map);
		}
		return JsonMapper.getInstance().toJson(set);
	}
}
